package com.pinyougou.sellergoods.service.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.pinyougou.group.Goods;
import com.pinyougou.mapper.*;
import com.pinyougou.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.pojo.TbGoodsExample.Criteria;
import com.pinyougou.sellergoods.service.GoodsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private TbGoodsMapper goodsMapper;
	@Autowired
	private TbGoodsDescMapper goodsDescMapper;
	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbSellerMapper sellerMapper;
	@Autowired
	private TbBrandMapper brandMapper;
	@Autowired
	private TbItemCatMapper itemCatMapper;
	/**
	 * 查询全部
	 */
	@Override
	public List<TbGoods> findAll() {
		return goodsMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbGoods> page=   (Page<TbGoods>) goodsMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	@Transactional
	public void add(Goods goods) {
		//1.向 tb_goods表中添加数据
		goods.getGoods().setAuditStatus("0");
		goodsMapper.insert(goods.getGoods());

		//2.向tb_goodsDesc表中添加数据
		//2.1)将上面添加的goods表的主键与下面的 goodsDesc表的主键绑定
		goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());
		//2.2)将goodsDesc表的数据添加到数据库中
		goodsDescMapper.insert(goods.getGoodsDesc());

		//3.添加sku商品表的数据
		List<TbItem> items = goods.getItems();
		inserItemList(goods, items);
	}

	private void inserItemList(Goods goods, List<TbItem> items) {
		//4.遍历数据
		for (TbItem item : items) {
			item.setTitle(goods.getGoods().getGoodsName());
			item.setCategoryid(goods.getGoods().getCategory3Id());
			//1.得到图像列表
			String itemImages = goods.getGoodsDesc().getItemImages();
			//2.将图像列表转换为java对象
			List<Map> maps = JSON.parseArray(itemImages, Map.class);
			//3.得到第一张图片并设置给当前的item对象
			item.setImage(maps.get(0).get("url")+"");

			//4.根据sellerId查询seller对象
			TbSeller tbSeller = sellerMapper.selectByPrimaryKey(goods.getGoods().getSellerId());
			//5.将seller商家名称设置给当前的item对象
			item.setSeller(tbSeller.getNickName());

			//5.查询品牌（根据品牌id）
			TbBrand tbBrand = brandMapper.selectByPrimaryKey(goods.getGoods().getBrandId());
			//6.为当前的item设置brand
			item.setBrand(tbBrand.getName());

			item.setUpdateTime(new Date());
			item.setCreateTime(new Date());
			//7.根据三级分类查询分类对象
			TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(goods.getGoods().getCategory3Id());
			//8.为当前的item设置分类名称
			item.setCategory(itemCat.getName());
			//9.设置goodsid
			item.setGoodsId(goods.getGoods().getId());

			//10.添加数据到tb_item表中（sku表）
			itemMapper.insert(item);
		}
	}
	/**
	 * 修改
	 */
	@Override
	@Transactional
	public void update(Goods goods){
		//1.修改spu商品表
		goodsMapper.updateByPrimaryKey(goods.getGoods());
		//2.修改商品描述表
		goodsDescMapper.updateByPrimaryKey(goods.getGoodsDesc());
		//3.修改sku商品列表
		//3.1)先在tb_item表中根据goodsId删除其中的记录（根据外键删除）
		TbItemExample example = new TbItemExample();
		TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andGoodsIdEqualTo(goods.getGoods().getId());
		itemMapper.deleteByExample(example);
		//3.2)添加新的sku商品信息列表
		inserItemList(goods,goods.getItems());
	}
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Goods findOne(Long id){
		//1.定义Goods组合对象
		Goods goods = new Goods();
		//2.根据商品id查询tbGoods（spu表）信息
		TbGoods tbGoods = goodsMapper.selectByPrimaryKey(id);
		//3.根据商品id查询tbGoodsDesc信息
		TbGoodsDesc goodsDesc = goodsDescMapper.selectByPrimaryKey(id);
		//4.根据商品id这个外键在tb_item表中查询sku商品列表信息
		//4.1)构造查询实例
		TbItemExample example = new TbItemExample();
		TbItemExample.Criteria criteria = example.createCriteria();
		//4.2）添加查询条件
		criteria.andGoodsIdEqualTo(id);
		//5.根据商品id进行sku列表
		List<TbItem> itemList = itemMapper.selectByExample(example);
		//6.将查询出的三样信息与goods组合对象进行绑定
		goods.setItems(itemList);
		goods.setGoodsDesc(goodsDesc);
		goods.setGoods(tbGoods);
		//7.返回
		return goods;
	}

	/**
	 * 批量删除(执行逻辑删除)
	 */
	@Override
	@Transactional
	public void delete(Long[] ids) {
		for(Long id:ids){
			//1.根据id查询商品对象
			TbGoods tbGoods = goodsMapper.selectByPrimaryKey(id);
			//2.修改isDelete的值为1
			tbGoods.setIsDelete("1");
			//3.执行修改操作
			goodsMapper.updateByPrimaryKey(tbGoods);
		}		
	}

	@Override
	public PageResult findPage(TbGoods goods, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbGoodsExample example=new TbGoodsExample();
		Criteria criteria = example.createCriteria();
		
		if(goods!=null){			
						if(goods.getSellerId()!=null && goods.getSellerId().length()>0){
				criteria.andSellerIdEqualTo(goods.getSellerId());
			}
			if(goods.getGoodsName()!=null && goods.getGoodsName().length()>0){
				criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");
			}
			if(goods.getAuditStatus()!=null && goods.getAuditStatus().length()>0){
				criteria.andAuditStatusLike("%"+goods.getAuditStatus()+"%");
			}
			if(goods.getIsMarketable()!=null && goods.getIsMarketable().length()>0){
				criteria.andIsMarketableLike("%"+goods.getIsMarketable()+"%");
			}
			if(goods.getCaption()!=null && goods.getCaption().length()>0){
				criteria.andCaptionLike("%"+goods.getCaption()+"%");
			}
			if(goods.getSmallPic()!=null && goods.getSmallPic().length()>0){
				criteria.andSmallPicLike("%"+goods.getSmallPic()+"%");
			}
			if(goods.getIsEnableSpec()!=null && goods.getIsEnableSpec().length()>0){
				criteria.andIsEnableSpecLike("%"+goods.getIsEnableSpec()+"%");
			}
			if(goods.getIsDelete()!=null && goods.getIsDelete().length()>0){
				criteria.andIsDeleteLike("%"+goods.getIsDelete()+"%");
			}
	
		}
		
		Page<TbGoods> page= (Page<TbGoods>)goodsMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void updateStatus(String status, String[] ids) {
		for (String id : ids) {
			//1.根据id查询商品
			TbGoods tbGoods = goodsMapper.selectByPrimaryKey(Long.parseLong(id));
			//2.修改状态
			tbGoods.setAuditStatus(status);
			//3.修改商品
			goodsMapper.updateByPrimaryKey(tbGoods);
		}
	}

}
